<template>
	<view>
		<swiper autoplay circular indicator-dots class="swiper">
			<swiper-item v-for="item in detail.gallery" :key="item.id">
				<image :src="item.img_url"></image>
			</swiper-item>
		</swiper>

		<rich-text :nodes="detail.info.goods_desc" class="rich-img"></rich-text>

		<view class="goods-carts">
			<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js';

	export default {
		data() {
			return {
				options: [{
					icon: 'chat',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
					info: 0,
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
					color: '#fff'
				}, {
					text: '立即购买',
					backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
					color: '#fff'
				}],
				detail: {}
			};
		},
		onLoad(options) {
			const id = options.id;
			this.queryDetail(id);
		},
		watch: {
			'$store.state.cart': {
				handler(newVal) {
					this.options[2].info = newVal.length;
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			onClick(e) {
				console.log(e, 'eeee')
				if (e.index === 2) {
					uni.switchTab({
						url: '/pages/cart/cart'
					});
				} else {
					uni.showToast({
						title: `点击${e.content.text}`,
						icon: 'none'
					});
				}
			},
			buttonClick(e) {
				console.log('111');
				if (!e.index) {
					const {
						id,
						name,
						list_pic_url: pic,
						retail_price: price
					} = this.detail.info;
					const goods = {
						id,
						name,
						pic,
						price
					};
					this.$store.commit('addCart', goods);
					uni.showToast({
						title: "加入购物车成功"
					})
				}
			},
			queryDetail(id) {
				request({
					url: 'goods/detailaction?openId=13216484256&id=1009024',
					data: {
						openId: '13216484256',
						id
					}
				}).then(res => {
					res.info.goods_desc = res.info.goods_desc.replace(/style=""/gi, '').replace(/\<img/gi,
						'<img style="width: 100%; height: auto;"');
					this.detail = res;

					console.log(res, 'res');
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 50px;
	}

	.swiper {
		height: 360rpx;

		image {
			width: 100%;
		}
	}

	.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
</style>
